---
category: String
created: '2020-05-16'
title: Sort the characters of a string in the alphabetical order
updated: '2021-10-13'
---

**JavaScript version**

```js sort.js
const sort = (str) =>
    str
        .split('')
        .sort((a, b) => a.localeCompare(b))
        .join('');
```

**TypeScript version**

```ts sort.ts
const sort = (str: string): string =>
    str
        .split('')
        .sort((a, b) => a.localeCompare(b))
        .join('');
```

**Examples**

```js examples.js
sort('hello world'); // dehllloorw
```
